package com.zutassociation.web.controller.system;

import com.zutassociation.common.core.controller.BaseController;
import com.zutassociation.common.core.domain.AjaxResult;
import com.zutassociation.common.core.page.TableDataInfo;
import com.zutassociation.common.utils.SecurityUtils;
import com.zutassociation.system.domain.Funds;
import com.zutassociation.system.service.FundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 经费 信息操作处理
 *
 * @author 黄玉东
 * @version 1.0
 * @date 2021/3/18 17:30
 */

@RestController
@RequestMapping("/funds")
public class FundsController extends BaseController {

    @Autowired
    private FundsService fundsService;


    /**
     * 获取经费列表
     */
//    @PreAuthorize("@ss.hasPermi('system:funds:list')")
    @GetMapping("/list")
    public TableDataInfo list(Funds funds) {
        startPage();
        List<Funds> list = fundsService.selectFundsList(funds);
        return getDataTable(list);
    }

    /**
     * 根据经费编号获取详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:funds:query')")
    @GetMapping(value = "/{fundsId}")
    public AjaxResult getInfo(@PathVariable("fundsId") Long fundsId) {
        return AjaxResult.success(fundsService.selectFundsById(fundsId));
    }

    @GetMapping(value = "/totalSum")
    public AjaxResult totalSum(Funds funds) {
        return AjaxResult.success(fundsService.selectTotalSum(funds));
    }


    /**
     * 新增经费
     */
//    @PreAuthorize("@ss.hasPermi('system:funds:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Funds funds) {
//        funds.setTotal(funds.getIncome()-funds.getPay());
        funds.setCreateBy(SecurityUtils.getUsername());
        return toAjax(fundsService.insertFunds(funds));
    }

    /**
     * 修改经费
     */
//    @PreAuthorize("@ss.hasPermi('system:funds:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Funds funds) {
        funds.setTotal(funds.getIncome()-funds.getPay());
        funds.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(fundsService.updateFunds(funds));
    }

    /**
     * 删除经费
     */
//    @PreAuthorize("@ss.hasPermi('system:funds:remove')")
    @DeleteMapping("/{fundsIds}")
    public AjaxResult remove(@PathVariable Long[] fundsIds) {
        return toAjax(fundsService.deleteFundsByIds(fundsIds));
    }

}
